package com.xh.bussiness.exam.service.impl;

import com.xh.bussiness.exam.bean.request.ExerciseRequest;
import com.xh.bussiness.exam.bean.request.StudentExerciseRequest;
import com.xh.bussiness.exam.model.Exercise;
import com.xh.bussiness.exam.dao.ExerciseMapper;
import com.xh.bussiness.exam.service.IExerciseService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.UserUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;
import java.util.List;
/**
 * <p>
 * 课堂练习记录表 服务实现类
 * </p>
 *
 * @author Pitt Cheung
 * @since 2021-08-26
 */
@Service
public class ExerciseServiceImpl extends BaseServiceImpl<ExerciseMapper, Exercise> implements IExerciseService {
    @Override
    public XhPage queryExercisePage(XhPage page, ExerciseRequest bean) {
        QueryWrapper qw=new QueryWrapper<Exercise>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return  (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryExercise(ExerciseRequest bean) {
        QueryWrapper qw=new QueryWrapper<Exercise>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(ExerciseRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }

    @Override
    public XhPage<ExerciseRequest> selectExercisePage(XhPage<ExerciseRequest> page, ExerciseRequest bean) {
        String userId = UserUtils.getCurrentUserId();
        if(userId==null){
            return null;
        }
        XhPage<ExerciseRequest> xhPage = this.dao.selectExercisePage(page,userId);
        if(xhPage.getRecords()!=null && xhPage.getRecords().size()>0){
            List<ExerciseRequest> list = xhPage.getRecords();
            for(ExerciseRequest exerciseRequest : list){
                String rate = this.dao.selectAttendanceRate(exerciseRequest);
                if(rate!=null){
                    exerciseRequest.setAttendanceRate(rate);
                }
            }
        }
        return xhPage;
    }

    @Override
    public String selectAttendanceRate(ExerciseRequest exerciseRequest) {
        return this.dao.selectAttendanceRate(exerciseRequest);
    }

    @Override
    public List<String> queryCurrIdsEvaluation(String classIds) {
        return this.dao.queryCurrIdsEvaluation(classIds);
    }
}
